package com.dingqb.ktdemo.base

fun main(a: Array<String>) {
    var b: Boolean = whenf1(null);
    println(b);
}

//分支判断1
fun whenf1(obj: Any?) = when (obj) {
    is String -> obj.startsWith("aa");
    is Int -> obj == 3;
    is Long -> obj == 4;
    else -> false;
}

// 分支判断2
fun whenf(x: Int) {
    when (x) {
        1 -> println("x==1")
        2 -> println("x==2");
        3, 4 -> println("3 or 4")
        in 10..20 -> println("x in 10 to 20");
        !in 30..40 -> println("x not in 30 - 40")
        else -> {
            println("else x===" + x)
        }
    }

}

//$符号 
fun dollar() {
    var a: Int = 1234;
    println("$a");//4
    var str: String = "abcd";
    println("${str.length + a}");// 4+1234=1238
    println("$str.length is ${str.length}");
    println(str.length);//
    println("$");
}

// 字符串
fun Str(): String {
    // 三个“”“ 多行字符串
    var st: String = """
 123
 abc
 !@#
 """;
    var sing: String = "1,2,3"
    println("$sing")
    var mul: String = "123\r\t"
    println("$mul")
    println("trim\n\n");
    var trim: String = "|  1 2  3  |";
    println(trim.trim());
    return st;
}

/**
列表集合。
 **/
fun arrayy() {
    // intarray
    println("ingArray usage")
    var intarray: IntArray = intArrayOf(1, 2, 3, 4, 5);
    for (i in 0 until intarray.size) {
        println(intarray[i]);
    }
    println("StringArray useage")
    // String
    var strArray: Array<String> = arrayOf("11", "22", "33");
    for (i in 0 until strArray.size) {
        println(strArray[i]);
    }
    println("doubleArray useage")
    var doubleArray = doubleArrayOf(1.1, 2.2, 3.3);
    for (i in 0..doubleArray.size - 1) {
        println(doubleArray[i]);
    }
    println("bean Array useage")
    var p: Person = Person(121);//121 =11*11 =110+11=121
    //23*54=1150+92 =1242 /

    println(p.age);
    println(TT().age);
}

class Person(age: Int) {
    var age: Int = age;
}

class TT {
    constructor();
    //构造函数
    var age: Int = 0xf;

}

/**
基本数据类型
 **/
fun basicData() {
    var b: Byte = 100;
    var i: Int = b.toInt();
    var l = i.toLong()
    var d = i.toDouble()
    var f = i.toFloat()
    var c = b.toChar()
    println(b);
    println(i);
    println(l);
    println(d);
    println(f);
    println(c);
}

//步长 step 
fun step() {
    for (i in 1..1000 step 10) {
        println(i);
    }
}


//null 非空判断
fun nulll() {
    var age: String? = null;
    var ages = age?.toInt();
    println(ages);
}


fun nulll2(obj: Any?) {
    if (obj is String) {
        println("String");
    } else if (obj is Int) {
        println("Int")
    } else {
        println("null");
    }


}

//fun add(a:Int,b:Int):Int=a+b;
//fun add(a:Int,b:Int)=a+b;// 自动识别返回值类型

fun add(a: Int, b: Int): Int {// 标准java版处理
    return a + b;
}

fun sum(a: Int, b: Int): Unit {// Unit 	返回值
    print(a + b);
}

var sumlambda: (Int, Int) -> Int = { a, b -> a + b };// lambda表达式

var add: (Int, Int) -> Int = { a, b -> a + b };//


fun vara(vararg v: Int) {//可变参数
    for (i: Int in v) {
        println(i);
    }
}



